import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home/index.vue'
// import Layout from '../views/Layout/index.vue'
// import Reg from '@/views/Reg'
// 导入组件
// import ArtCategory from '@/views/ArtCategory'
// import ArtList from '@/views/ArtList'
// import UserInfo from '@/views/UserInfo'
// import ChangeAvatar from '@/views/ChangeAvatar'
// import ResetPwd from '@/views/ResetPwd'

const Login = () => import('@/views/Login')
const Reg = () => import('@/views/Reg')
const Layout = () => import('@/views/Layout')
const Home = () => import('@/views/Home')
const ArtCategory = () => import('@/views/ArtCategory')
const ArtList = () => import('@/views/ArtList')
const UserInfo = () => import('@/views/UserInfo')
const ChangeAvatar = () => import('@/views/ChangeAvatar')
const ResetPwd = () => import('@/views/ResetPwd')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'Home',
        component: Home
      },
      { path: '/artcategory', component: ArtCategory },
      { path: '/artlist', component: ArtList },
      { path: '/userinfo', component: UserInfo },
      { path: '/changeavatar', component: ChangeAvatar },
      { path: '/resetpwd', component: ResetPwd }
    ]
  },
  {
    path: '/login',
    name: 'login',
    // component: () => import('../views/Login/index.vue')
    component: Login
  },
  { path: '/reg', component: Reg }

]

const router = new VueRouter({
  routes
})

// 路由前置导航守卫
// 白名单列表
const whiteList = ['/login', '/reg']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
